package com.manage.adapter;

import java.util.ArrayList;

import com.manage.DeliveryOrderDetailActivity;
import com.manage.ManageAcceptedActivity;
import com.manage.OrderDetailActivity;
import com.manage.PendingDeliveryOrdersActivity;
import com.manage.R;
import com.manage.base.BaseActivity;
import com.manage.model.Order;
import com.manage.model.OrderItem;
import com.manage.widgets.RemarkDialog;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

/**
 * 待配送订单listview的item设置
 * @author liyoro
 * @date 2012-5-30
 */
public class PendingDeliveryOrderAdapter extends BaseAdapter {
	private LayoutInflater layoutInflater;
	private ArrayList<Order> orderList = new ArrayList();
	private Context context;

	private boolean clearList;
	
	public PendingDeliveryOrderAdapter(Context paramContext) {
		this.context = paramContext;
		this.layoutInflater = LayoutInflater.from(paramContext);
	}
	
	@Override
	public int getCount() {
		return this.orderList.size();
	}

	@Override
	public Object getItem(int position) {
		return this.orderList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder localViewHolder;
		final Order localOrder;

		if (convertView == null) {
			localViewHolder = new ViewHolder();
			
			convertView = this.layoutInflater.inflate(R.layout.item_pending_delivery_order, null);
			localViewHolder.clientName = (TextView) convertView.findViewById(R.id.client_name);
			localViewHolder.orderTime = (TextView) convertView.findViewById(R.id.order_time);
			localViewHolder.phoneNumber = (TextView) convertView.findViewById(R.id.phone_number);
			localViewHolder.clientAddress = (TextView) convertView.findViewById(R.id.client_address);
			localViewHolder.clientAddition = (TextView) convertView.findViewById(R.id.client_other);
			localViewHolder.orderContent = (TextView) convertView.findViewById(R.id.order_content);
			
			localViewHolder.btnModify = (Button) convertView.findViewById(R.id.btn_modify_order);
			localViewHolder.btnDeliver = (Button) convertView.findViewById(R.id.btn_delivery_order);
			
			convertView.setTag(localViewHolder);
		} else {
			localViewHolder = (ViewHolder) convertView.getTag();
		}
		
		localOrder = (Order)getItem(position);

		/**
		 * 如果用户有做备注，变为链接，并且可以点击查看
		 */
		if(localOrder.getRemark() != "") {
			localViewHolder.clientName.setTextColor(context.getResources().getColor(R.color.blue));
			localViewHolder.clientName.setText(Html.fromHtml("<u>"+ localOrder.getClientName() +"</u>"));
			
			localViewHolder.clientName.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					RemarkDialog remarkDialog = new RemarkDialog(context, localOrder.getClientName(), localOrder.getPhoneNumber());
					remarkDialog.addRemark(localOrder.getRemark());
					remarkDialog.show();
				}
			});
		} else {
			localViewHolder.clientName.setText(localOrder.getClientName());
		}
		
		localViewHolder.orderTime.setText(localOrder.getOrderTime());
		
		localViewHolder.phoneNumber.setText(Html.fromHtml("<u>"+ localOrder.getPhoneNumber() +"</u>"));
		
		localViewHolder.phoneNumber.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) { 
				Intent telIntent = new Intent(  
				                Intent.ACTION_CALL, Uri.parse("tel:"+ localOrder.getPhoneNumber()));
				context.startActivity(telIntent);
			} 
		});
		
		localViewHolder.clientAddress.setText(localOrder.getClientAddress());
		localViewHolder.clientAddition.setText(localOrder.getClientAddition());
		
		String content = "";
		
		ArrayList<OrderItem> dish = localOrder.getDishes();
		for(int i=0; i<dish.size(); i++) {
			OrderItem item = dish.get(i);
			
			content += item.getCaipin_name() + item.getCaipin_count() + "份； ";
		}
		localViewHolder.orderContent.setText(content);
		
		localViewHolder.btnModify.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 跳转到订单详细页面
				Intent intent = new Intent();
				intent.setClass(context, DeliveryOrderDetailActivity.class);
				
				// 通过Bundle来获取数据,通过key-Value的方式放入数据
				Bundle bundle = new Bundle();
				bundle.putSerializable(ManageAcceptedActivity.SER_KEY, localOrder);
				intent.putExtras(bundle);   
				
				context.startActivity(intent);
			}
		});
		
		localViewHolder.btnDeliver.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Message msg = new Message();
				msg.what = BaseActivity.DELIVERYORDER;
				
				Bundle bundle = new Bundle();
				bundle.putString("orderid", localOrder.getOrderID());
				msg.setData(bundle);
				
				PendingDeliveryOrdersActivity.self.handler.sendMessage(msg);
			}
		});
		
		return convertView;
	}

	public void setValue(ArrayList<Order> paramArrayList) {
		if(getClear()) {
			this.orderList.clear();
		}
		
		if(paramArrayList != null) {
			this.orderList.addAll(paramArrayList);
		} 
		
		notifyDataSetChanged();
	}

	/**
	 * 设置是否清空list中数据，防止分页数据没了
	 * @param isClear
	 */
	public void setClear(boolean isClear) {
		this.clearList = isClear;
	}
	
	public boolean getClear() {
		return this.clearList==true?true:false;
	}
	
	static class ViewHolder {
		TextView orderTime; // 下单时间
		TextView clientName; // 客户名
		TextView phoneNumber; // 客户电话号码
		TextView clientAddress; // 送餐地址
		TextView clientAddition; // 补充
		TextView orderContent; // 订餐内容

		Button btnModify;
		Button btnDeliver;
	}
}
